var Network = new Class(
{

Extends: Game,

	initialize: function()
	{

		//application stuff
		this.mMessageServerExit = 3;
		this.mMessageConnect     = -101;
		this.mMessageDisconnect  = -102;

		//game stuff
		this.mCommandKey          = 1;
		this.mCommandFrameTime = 2;

		this.mMessageFrame = 1;

		this.mMessageAddShape    = -103;
		this.mMessageRemoveShape = -104;

		this.mOutgoingSequence = 1; 
		this.mRunNetworkTime = 0;

        //keys
        mKeyUp = 1;
        mKeyDown = 2;
        mKeyLeft = 4;
        mKeyRight = 8;
        mKeyCounterClockwise = 16;
        mKeyClockwise = 32;

        //input
        mKeyCurrent = 0;
        mKeyLast = 0;

        //sequence
        mOutgoingSequence               = 1;

        //time
        mRunNetworkTime = 0.0f;


       		this.parent();

	},
   	
	update: function()
        {
		this.parent();
                if (this.mOn)
		{
			this.sendByteBuffer();
		}
	},

/*	
 mRunNetworkTime += mApplicationBreslin->getRenderTime() * 1000.0f;

        // Framerate is too high
        if(mRunNetworkTime > (1000 / 60))
        {
                // Build delta-compressed move command
                int flags = 0;

                //if key has not been changed return having done nothing
                if(mKeyLast != mKeyCurrent)
                {
                        flags |= mCommandKey;
                }
                else
                {
                        return;
                }

                //create byteBuffer
                ByteBuffer* byteBuffer = new ByteBuffer();

                //WRITE: type
                byteBuffer->WriteByte(mMessageFrame);

                //WRITE: sequence
                byteBuffer->WriteShort(mOutgoingSequence);

                mOutgoingSequence++; //increase for next time...

                // Add to the message
                byteBuffer->WriteByte(flags);

                if(flags & mCommandKey)
                {
                        //WRITE: key
                        byteBuffer->WriteByte(mKeyCurrent);
                }

                //set 'last' commands for diff
                mKeyLast = mKeyCurrent;

                // Send the packet
                mApplicationBreslin->mNetwork->send(byteBuffer);

                mRunNetworkTime = 0.0f;
        }
*/
	sendByteBuffer: function()
	{
 		this.mRunNetworkTime = this.mDeltaTime * 1000.0;		
 		// Framerate is too high
        	if(this.mRunNetworkTime > (1000 / 60))
        	{
			this.log('good');
                	// Build delta-compressed move command
                	flags = 0;

                	//if key has not been changed return having done nothing
                	if(this.mKeyLast != this.mKeyCurrent)
                	{
                        	flags |= this.mCommandKey;
                	}
                	else
                	{
                        	return;
                	}

			//send the packet
			this.postKeyToServer(this.mMessageFrame,this.mOutgoingSequence,flags,this.mKeyCurrent);
		}
		else
		{
			this.log('bad');
		}
	},

	postKeyToServer: function(key,outgoingSequence)
	{
		var xmlhttp;
		if (window.XMLHttpRequest)
  		{
			// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{
			// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    			{
    				document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    			}
  		}
	

		xmlhttp.open("POST","postKeyToServer.php",true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	
		if (key == 'i')
		{	
			xmlhttp.send("k=i&s=this.mOutgoingSequence");
		}
		this.mOutgoingSequence++;
	
	},
/*
   mKeyCurrent = 0;

        if (mApplicationBreslin->getKeyboard()->isKeyDown(OIS::KC_I)) // Forward
   {
                mKeyCurrent |= mKeyUp;
   }

   if (mApplicationBreslin->getKeyboard()->isKeyDown(OIS::KC_K)) // Backward
   {
                mKeyCurrent |= mKeyDown;
   }

        if (mApplicationBreslin->getKeyboard()->isKeyDown(OIS::KC_J)) // Left
   {
                mKeyCurrent |= mKeyLeft;
   }

   if (mApplicationBreslin->getKeyboard()->isKeyDown(OIS::KC_L)) // Right
   {
                mKeyCurrent |= mKeyRight;
   }

        if (mApplicationBreslin->getKeyboard()->isKeyDown(OIS::KC_Z)) // Rotate -Yaw(counter-clockwise)
   {
                mKeyCurrent |= mKeyCounterClockwise;
   }

   if (mApplicationBreslin->getKeyboard()->isKeyDown(OIS::KC_X)) // Right + Yaw(clockwise)
   {
                mKeyCurrent |= mKeyClockwise;
   }
*/
	processInput function()
	{
		this.mKeyCurrent = 0;
		if (GAME.mKeyLeft)
		{
			this.mKeyCurrent |= this.mKeyLeft;
		}	
		if (GAME.mKeyRight)
		{
			this.mKeyCurrent |= this.mKeyRight;
		}	
		if (GAME.mKeyUp)
		{
			this.mKeyCurrent |= this.mKeyUp;
		}	
		if (GAME.mKeyDown)
		{
			this.mKeyCurrent |= this.mKeyDown;
		}	
	},

        /******************************* CONTROLS  *************/
        keyDown: function(event)
        {
                //left
                if (event.key == 'left')
                {
                        GAME.mKeyLeft = true;
                }
                if (event.key == 'j')
                {
                        GAME.mKeyLeft = true;
                }

                //right
                if (event.key == 'right')
                {
                        GAME.mKeyRight = true;
                }
                if (event.key == 'l')
                {
                        GAME.mKeyRight = true;
                }

                //up
                if (event.key == 'up')
                {
                        GAME.mKeyUp = true;
                }
                if (event.key == 'i')
                {
			GAME.log('i up');	
                        GAME.mKeyUp = true;
                }

                //down
                if (event.key == 'down')
                {
                        GAME.mKeyDown = true;
                }
                if (event.key == 'k')
                {
                        GAME.mKeyDown = true;
                }


                if (event.key == 'space')
                {
                        GAME.mKeySpace = true;
                }
                if (event.key == '0')
                {
                        GAME.mKey0 = true;
                }
                if (event.key == '1')
                {
                        GAME.mKey1 = true;
                }
                if (event.key == '2')
                {
                        GAME.mKey2 = true;
                }
                if (event.key == '3')
                {
                        GAME.mKey3 = true;
                }
                if (event.key == '4')
                {
                        GAME.mKey4 = true;
                }
                if (event.key == '5')
                {
                        GAME.mKey5 = true;
                }
                if (event.key == '6')
                {
                        GAME.mKey6 = true;
                }
                if (event.key == '7')
                {
                        GAME.mKey7 = true;
                }
                if (event.key == '8')
                {
                        GAME.mKey8 = true;
                }
                if (event.key == '9')
                {
                        GAME.mKey9 = true;
                }
        },
	
	keyUp: function(event)
        {
                //left
                if (event.key == 'left')
                {
                        GAME.mKeyLeft = false;
                }
                if (event.key == 'j')
                {
                        GAME.mKeyLeft = false;
                }

                //right
                if (event.key == 'right')
                {
                        GAME.mKeyRight = false;
                }
                if (event.key == 'l')
                {
                        GAME.mKeyRight = false;
                }

                //up
                if (event.key == 'up')
                {
                        GAME.mKeyUp = false;
                }
                if (event.key == 'i')
                {
                        GAME.mKeyUp = false;
                }

                //down
                if (event.key == 'down')
                {
                        GAME.mKeyDown = false;
                }
                if (event.key == 'k')
                {
                        GAME.mKeyDown = false;
                }


                if (event.key == 'space')
                {
                        GAME.mKeySpace = false;
                }
                if (event.key == '0')
                {
                        GAME.mKey0 = false;
                }
                if (event.key == '1')
                {
                        GAME.mKey1 = false;
                }
                if (event.key == '2')
                {
                        GAME.mKey2 = false;
                }
                if (event.key == '3')
                {
                        GAME.mKey3 = false;
                }
                if (event.key == '4')
                {
                        GAME.mKey4 = false;
                }
                if (event.key == '5')
                {
                        GAME.mKey5 = false;
                }
                if (event.key == '6')
                {
                        GAME.mKey6 = false;
                }
                if (event.key == '7')
                {
                        GAME.mKey7 = false;
                }
                if (event.key == '8')
                {
                        GAME.mKey8 = false;
                }
                if (event.key == '9')
                {
                        GAME.mKey9 = false;
                }
        }



});

